/**
  ******************************************************************************
  * @file    gt32f030_i2c.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025
  *       
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GT32F030_I2C_H    
#define __GT32F030_I2C_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "gt32f030.h"
/** @addtogroup GT32F030_StdPeriph_Driver
  * @{
  */

/** @addtogroup I2C
  * @{
  */ 

/* Exported macro ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/** @defgroup GPIO_Exported_Constants
  * @{
  */ 

#define IS_I2C_ADDRESS(ADDRESS)    ((ADDRESS) <= 0x7F)
#define IS_I2C_BUADCFG(BUADCFG)     (((BUADCFG) >= 0x1) && ((BUADCFG) <= 0x7F))

/**
  * @}
  */

/* Exported types ------------------------------------------------------------*/
/** 
  * @brief   I2C Init structure definition  
  */
typedef struct
{
	uint8_t I2C_BuadCfg;  
	FunctionalState I2C_BuadCmd;
	FunctionalState I2C_HighSpeedCmd; 
	uint16_t I2C_Address;                
}I2C_InitTypeDef;

/** 
  * @brief  I2C OperationConfig define 
  */  
#define I2C_OptStart_Enable           ((uint32_t)1)
#define I2C_OptStart_Disable          ((uint32_t)0x00000000)
#define IS_I2C_OPTSTART(OPTSTART) (((OPTSTART) == I2C_OptStart_Enable) || \
                                   ((OPTSTART) == I2C_OptStart_Disable))

#define I2C_OptStop_Enable           ((uint32_t)1)
#define I2C_OptStop_Disable          ((uint32_t)0x00000000)
#define IS_I2C_OPTSTOP(OPTSTOP)  (((OPTSTOP) == I2C_OptStop_Enable) || \
                                   ((OPTSTOP) == I2C_OptStop_Disable))

#define I2C_OptAck_Enable           ((uint32_t)1)
#define I2C_OptAck_Disable          ((uint32_t)0x00000000)
#define IS_I2C_OPTACK(OPTACK)    (((OPTACK) == I2C_OptAck_Enable) || \
                                  ((OPTACK) == I2C_OptAck_Disable))

// 主发送模式状态码
#define I2C_MT_START_SENT ((uint32_t)0x00000008)          // 已发送起始信号
#define I2C_MT_REPEATED_START_SENT ((uint32_t)0x00000010) // 已发送重复起始信号
#define I2C_MT_SLA_W_ACK ((uint32_t)0x00000018)  // 已发送 SLA+W，已接收 ACK
#define I2C_MT_SLA_W_NACK ((uint32_t)0x00000020) // 已发送 SLA+W，已接收 NACK
#define I2C_MT_DATA_ACK ((uint32_t)0x00000028)   // 已发送数据，已接收 ACK
#define I2C_MT_DATA_NACK ((uint32_t)0x00000030)  // 已发送数据，已接收 NACK
#define I2C_MT_ARB_LOST_SLAW_OR_DATA                                           \
  ((uint32_t)0x00000038) // 主机在发送 SLA+W 或数据时丢失仲裁

// 主接收模式状态码
#define I2C_MR_START_SENT ((uint32_t)0x00000008)          // 已发送起始信号
#define I2C_MR_REPEATED_START_SENT ((uint32_t)0x00000010) // 已发送重复起始信号
#define I2C_MR_ARB_LOST_SLAR_OR_NACK                                           \
  ((uint32_t)0x00000038) // 主机在发送 SLA+R 或回应 NACK 时丢失仲裁
#define I2C_MR_SLA_R_ACK ((uint32_t)0x00000040)  // 已发送 SLA+R，已接收 ACK
#define I2C_MR_SLA_R_NACK ((uint32_t)0x00000048) // 已发送 SLA+R，已接收 NACK
#define I2C_MR_DATA_RECEIVED_ACK                                               \
  ((uint32_t)0x00000050) // 已接收数据字节，ACK 已返回
#define I2C_MR_DATA_RECEIVED_NACK                                              \
  ((uint32_t)0x00000058) // 已接收数据字节，NACK 已返回

// 从发送模式状态码
#define I2C_ST_SLA_R_ACK ((uint32_t)0x000000A8) // 已接收自身 SLA+R，已返回 ACK
#define I2C_ST_ARB_LOST_SLA_R_ACK                                              \
  ((uint32_t)0x000000B0) // 主机在 SLA+R 阶段丢失仲裁，已返回 ACK
#define I2C_ST_DATA_SENT_ACK                                                   \
  ((uint32_t)0x000000B8) // 已发送数据字节，已接收 ACK
#define I2C_ST_DATA_SENT_NACK                                                  \
  ((uint32_t)0x000000C0) // 已发送数据字节，已接收 NACK
#define I2C_ST_LAST_DATA_SENT_ACK                                              \
  ((uint32_t)0x000000C8) // 最后一个数据字节已发送，已接收 ACK

// 从接收模式状态码
#define I2C_SR_SLA_W_ACK ((uint32_t)0x00000060) // 已接收自身 SLA+W，已返回 ACK
#define I2C_SR_ARB_LOST_SLA_W_ACK                                              \
  ((uint32_t)0x00000068) // 主机在 SLA+W 阶段丢失仲裁，已返回 ACK
#define I2C_SR_GENERAL_CALL_ACK                                                \
  ((uint32_t)0x00000070) // 已接收广播地址，已返回 ACK
#define I2C_SR_ARB_LOST_GENERAL_CALL_ACK                                       \
  ((uint32_t)0x00000078) // 主机在广播地址阶段丢失仲裁，已返回 ACK
#define I2C_SR_DATA_RECEIVED_ACK                                               \
  ((uint32_t)0x00000080) // 前一次寻址使用自身地址，已接收数据并返回 ACK
#define I2C_SR_DATA_RECEIVED_NACK                                              \
  ((uint32_t)0x00000088) // 前一次寻址使用自身地址，已接收数据并返回 NACK
#define I2C_SR_GENERAL_DATA_ACK                                                \
  ((uint32_t)0x00000090) // 前一次寻址使用广播地址，已接收数据并返回 ACK
#define I2C_SR_GENERAL_DATA_NACK                                               \
  ((uint32_t)0x00000098) // 前一次寻址使用广播地址，已接收数据并返回 NACK
#define I2C_SR_STOP_OR_RESTART                                                 \
  ((uint32_t)0x000000A0) // 从机接收数据时收到停止或重复起始条件

// 其他状态码
#define I2C_STATUS_NO_INFO ((uint32_t)0x000000F8)   // 无状态信息
#define I2C_STATUS_BUS_ERROR ((uint32_t)0x00000000) // 总线错误或未定义状态

#define IS_I2C_FLAG_STATUS(I2C_STATUS)                                         \
  (((I2C_STATUS) == I2C_STATUS_NO_INFO) ||                                     \
   ((I2C_STATUS) == I2C_STATUS_BUS_ERROR) ||                                   \
   ((I2C_STATUS) == I2C_MT_START_SENT) ||                                      \
   ((I2C_STATUS) == I2C_MT_REPEATED_START_SENT) ||                             \
   ((I2C_STATUS) == I2C_MT_SLA_W_ACK) ||                                       \
   ((I2C_STATUS) == I2C_MT_SLA_W_NACK) ||                                      \
   ((I2C_STATUS) == I2C_MT_DATA_ACK) ||                                        \
   ((I2C_STATUS) == I2C_MT_DATA_NACK) ||                                       \
   ((I2C_STATUS) == I2C_MT_ARB_LOST_SLAW_OR_DATA) ||                           \
   ((I2C_STATUS) == I2C_MR_START_SENT) ||                                      \
   ((I2C_STATUS) == I2C_MR_REPEATED_START_SENT) ||                             \
   ((I2C_STATUS) == I2C_MR_ARB_LOST_SLAR_OR_NACK) ||                           \
   ((I2C_STATUS) == I2C_MR_SLA_R_ACK) ||                                       \
   ((I2C_STATUS) == I2C_MR_SLA_R_NACK) ||                                      \
   ((I2C_STATUS) == I2C_MR_DATA_RECEIVED_ACK) ||                               \
   ((I2C_STATUS) == I2C_MR_DATA_RECEIVED_NACK) ||                              \
   ((I2C_STATUS) == I2C_ST_SLA_R_ACK) ||                                       \
   ((I2C_STATUS) == I2C_ST_ARB_LOST_SLA_R_ACK) ||                              \
   ((I2C_STATUS) == I2C_ST_DATA_SENT_ACK) ||                                   \
   ((I2C_STATUS) == I2C_ST_DATA_SENT_NACK) ||                                  \
   ((I2C_STATUS) == I2C_ST_LAST_DATA_SENT_ACK) ||                              \
   ((I2C_STATUS) == I2C_SR_SLA_W_ACK) ||                                       \
   ((I2C_STATUS) == I2C_SR_ARB_LOST_SLA_W_ACK) ||                              \
   ((I2C_STATUS) == I2C_SR_GENERAL_CALL_ACK) ||                                \
   ((I2C_STATUS) == I2C_SR_ARB_LOST_GENERAL_CALL_ACK) ||                       \
   ((I2C_STATUS) == I2C_SR_DATA_RECEIVED_ACK) ||                               \
   ((I2C_STATUS) == I2C_SR_DATA_RECEIVED_NACK) ||                              \
   ((I2C_STATUS) == I2C_SR_GENERAL_DATA_ACK) ||                                \
   ((I2C_STATUS) == I2C_SR_GENERAL_DATA_NACK) ||                               \
   ((I2C_STATUS) == I2C_SR_STOP_OR_RESTART)))

/* Exported functions --------------------------------------------------------*/
void I2C_DeInit(void);
void I2C_Init(I2C_InitTypeDef* I2C_InitStruct);
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);

void I2C_OperationConfig(uint8_t Enable,uint8_t Sta,uint8_t Sto,uint8_t Ack);
void I2C_GeneralCallCmd(FunctionalState NewState);
void I2C_SendData(uint8_t Data);
uint8_t I2C_ReceiveData(void);

ITStatus I2C_GetIT(void);
void I2C_ClearIT(void);

uint16_t I2C_GetStatus(void);
FlagStatus I2C_GetFlagStatus(uint32_t I2C_Flag);

#ifdef __cplusplus
}
#endif

#endif /*__GT32F030_I2C_H */

/**
  * @}
  */ 

/**
  * @}
  */ 

/************************ (C) COPYRIGHT Giantec Semicondutor Inc *****END OF FILE****/
